package com.example.healkep.fragment.weight;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.fragment.app.Fragment;

import com.example.healkep.R;
import com.example.healkep.echarts.EchartOptionUtil;
import com.example.healkep.echarts.EchartView;
import com.example.healkep.fragment.ConnectNulls;

public class FragmentRecomWeight extends Fragment {

    private double[] weight = new double[43];
    private double[] weight_new = new double[43];
    private Double[] gainhigh = new Double[43];
    private Double[] gainlow = new Double[43];
    private int preg_num;  //孕胎数
    private double weight_ori;   //初始体重
    private double BMI;
    private Double[] user = new Double[43];   //对象数组（用户实际值）
    private EchartView lineChart;

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        final Intent intent = getActivity().getIntent();
        weight = intent.getDoubleArrayExtra("weight");
        preg_num = intent.getIntExtra("preg_num", 1);   //孕胎数，默认为1
        BMI = intent.getDoubleExtra("BMI", 20);  //BMI值（默认正常）
        weight_ori = weight[0];  //初始体重
        //*************************************
        weight_ori = 50;  //无传入值时测试用，实际运行时注释掉
        //*************************************

        //体重值改为体重增长值
        for(int i=1; i<43; i++)
        {
            if(weight[i] != 0)  weight_new[i] = weight[i] - weight_ori;
            else weight_new[i] = 0;
        }

        //用户信息改为Double对象数组
        for(int i=1; i<43; i++)
        {
            if(weight_new[i] == 0)  user[i] = null;
            else user[i] = weight_new[i];
        }
        ConnectNulls.Transform_nums(user);

        //单胎情况
        if(preg_num == 1) {
            //体重不足（13.0-18.5）
            if(BMI < 18.5) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainhigh[i] = 0.19 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainhigh[i] = 2.28 + 0.58 * (i - 12);
                }

                gainlow[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainlow[i] = 0.06 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainlow[i] = 0.72 + 0.44 * (i - 12);
                }
            }
            //体重标准（11.5-16）
            else if(BMI > 18.5 && BMI < 24.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainhigh[i] = 0.17 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainhigh[i] = 2.04 + 0.5 * (i - 12);
                }

                gainlow[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainlow[i] = 0.14 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainlow[i] = 1.68 + 0.35 * (i - 12);
                }
            }
            //体重超重(7-11.5)
            else if(BMI > 25.0 && BMI < 29.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainhigh[i] = 0.19 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainhigh[i] = 2.28 + 0.33 * (i - 12);
                }

                gainlow[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainlow[i] = 0.05 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainlow[i] = 0.6 + 0.23 * (i - 12);
                }
            }
            //体重肥胖(5-9)
            else if(BMI > 25.0 && BMI < 29.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainhigh[i] = 0.12 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainhigh[i] = 1.44 + 0.33 * (i - 12);
                }

                gainlow[0] = 0.0;
                for (int i = 1; i < 13; i++) {
                    gainlow[i] = 0.02 * i;
                }
                for (int i = 13; i < 43; i++) {
                    gainlow[i] = 0.24 + 0.17 * (i - 12);
                }
            }
        }

        //双胎情况
        if(preg_num == 2) {
            //体重不足（13.0-18.5）
            if (BMI < 18.5) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainhigh[i] = 0.9 * i;
                }
                gainlow[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainlow[i] = 0.75 * i;
                }
            }
            //体重标准（11.5-16）
            else if (BMI > 18.5 && BMI < 24.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainhigh[i] = 0.88 * i;
                }
                gainlow[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainlow[i] = 0.63 * i;
                }
            }
            //体重超重(7-11.5)
            else if (BMI > 25.0 && BMI < 29.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainhigh[i] = 0.81 * i;
                }
                gainlow[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainlow[i] = 0.52 * i;
                }
            }
            //体重肥胖(5-9)
            else if (BMI > 25.0 && BMI < 29.9) {
                gainhigh[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainhigh[i] = 0.65 * i;
                }
                gainlow[0] = 0.0;
                for (int i = 1; i < 43; i++) {
                    gainlow[i] = 0.40 * i;
                }
            }
        }


        final View view = inflater.inflate(R.layout.weight_fragment_recom,
                container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        lineChart = view.findViewById(R.id.lineChart);
        lineChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshLineChart();
            }
        });
    }

    private void refreshLineChart(){
        Object[] x = new Object[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
                "31", "32", "33", "34", "35", "36", "37", "38", "39", "40","41", "42"
        };
        Object[] y1 = new Object[]{
                gainhigh[1], gainhigh[2], gainhigh[3], gainhigh[4], gainhigh[5], gainhigh[6],gainhigh[7],gainhigh[8],gainhigh[9],gainhigh[10],
                gainhigh[11], gainhigh[12], gainhigh[13], gainhigh[14], gainhigh[15], gainhigh[16],gainhigh[17],gainhigh[18],gainhigh[19],gainhigh[20],
                gainhigh[21], gainhigh[22], gainhigh[23], gainhigh[24], gainhigh[25], gainhigh[26],gainhigh[27],gainhigh[28],gainhigh[29],gainhigh[30],
                gainhigh[31], gainhigh[32], gainhigh[33], gainhigh[34], gainhigh[35], gainhigh[36],gainhigh[37],gainhigh[38],gainhigh[39],gainhigh[40],
                gainhigh[41], gainhigh[42]
        };
        Object[] y2 = new Object[]{
                user[1], user[2], user[3], user[4], user[5], user[6], user[7], user[8], user[9], user[10],
                user[11], user[12], user[13], user[14], user[15], user[16], user[17], user[18], user[19], user[20],
                user[21], user[22], user[23], user[24], user[25], user[26], user[27], user[28], user[29], user[30],
                user[31], user[32], user[33], user[34], user[35], user[36], user[37], user[38], user[39], user[40],
                user[41], user[42]
        };
        /*
        Object[] y2 = new Object[]{
                user[1], user[2], user[3], user[4], user[5], user[6], user[7], user[8], user[9], user[10],
                weight_new[11], weight_new[12], weight_new[13], weight_new[14], weight_new[15], weight_new[16], weight_new[17], weight_new[18], weight_new[19], weight_new[20],
                weight_new[21], weight_new[22], weight_new[23], weight_new[24], weight_new[25], weight_new[26], weight_new[27], weight_new[28], weight_new[29], weight_new[30],
                weight_new[31], weight_new[32], weight_new[33], weight_new[34], weight_new[35], weight_new[36], weight_new[37], weight_new[38], weight_new[39], weight_new[40],
                weight_new[41],weight_new[42]
        };
        */
        Object[] y3 = new Object[]{
                gainlow[1], gainlow[2], gainlow[3], gainlow[4], gainlow[5], gainlow[6],gainlow[7],gainlow[8],gainlow[9],gainlow[10],
                gainlow[11], gainlow[12], gainlow[13], gainlow[14], gainlow[15], gainlow[16],gainlow[17],gainlow[18],gainlow[19],gainlow[20],
                gainlow[21], gainlow[22], gainlow[23], gainlow[24], gainlow[25], gainlow[26],gainlow[27],gainlow[28],gainlow[29],gainlow[30],
                gainlow[31], gainlow[32], gainlow[33], gainlow[34], gainlow[35], gainlow[36],gainlow[37],gainlow[38],gainlow[39],gainlow[40],
                gainlow[41], gainlow[42]
        };
        lineChart.refreshEchartsWithOption(EchartOptionUtil.getLineChartOptionsW3(x, y1, y2, y3));
    }
}
